<?php 
	session_start();
	$var1= $_SESSION["registrado"];
	$var2= $_SESSION["administrador"];
	if ($var1 == null): {
		echo ("User not registered");
	} elseif ($var2 == 0): { 
			echo ("User without permissions");
	}elseif (empty($_POST['datepicker']) && empty($_POST['datepicker2'])&& empty($_POST['datepicker3'])&& empty($_POST['datepicker4'])) :{ 
	    echo ("You can only access this page from graphs and fields can´t be null");
	}else : {
?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<?php include ('configuration.php'); ?>	
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet"	media="screen">

<title>My project manager</title>



<style type="text/css">
  body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
  .sidebar-nav {
    padding: 9px 0;
  
  }
    .container {
    padding-top: 100px;
  }

    .finish {
        padding-top: 200px;
        padding-left: 25px;
        padding-right: 25px;
  }
  

  
  @media (max-width: 980px) {
    /* Enable use of floated navbar text */
    .navbar-text.pull-right {
      float: none;
      padding-left: 5px;
      padding-right: 5px;
    }
  }
</style>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>

<?php 
	$no_actualizar = 0;
	$message = 0;
	$initial_date1=$_POST['datepicker'];
	$initial_date2=$_POST['datepicker2'];
	$final_date1=$_POST['datepicker3'];
	$final_date2=$_POST['datepicker4'];
	$_SESSION['ini1'] = $initial_date1;
	$_SESSION['ini2'] = $initial_date2;
	$_SESSION['fin1'] = $final_date1;
	$_SESSION['fin2'] = $final_date2;
	
	$anyoIni1 = substr($initial_date1, 0, 4);
	$aux1 = substr($initial_date1, 5, 6);
	$mesIni1 = substr($aux1, 0, 2);
	$diaIni1 = substr($initial_date1, 8, 9);
	
	$anyoIni2 = substr($initial_date2, 0, 4);
	$aux2 = substr($initial_date2, 5, 6);
	$mesIni2 = substr($aux2, 0, 2);
	$diaIni2 = substr($initial_date2, 8, 9);
	
	$anyoFin1 = substr($final_date1, 0, 4);
	$aux3 = substr($final_date1, 5, 6);
	$mesFin1 = substr($aux3, 0, 2);
	$diaFin1 = substr($final_date1, 8, 9);
	
	$anyoFin2 = substr($final_date2, 0, 4);
	$aux4 = substr($final_date2, 5, 6);
	$mesFin2 = substr($aux4, 0, 2);
	$diaFin2 = substr($final_date2, 8, 9);
	
	//ninguna fecha puede ser nula
	if ($initial_date1 == null || $initial_date2 == null || $final_date1 == null || $final_date2 == null) {
		$no_actualizar=1;
		echo "<h1 style='position:absolute;top:100px;left:50px;font-weight:bold'><font size='5'><u>Dates can't be null</u></font></h1>"; 
		echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/graphs.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
   	} else {
	   	//la segunda fecha de cada rango debe ser mayor que la primera
		if( !($anyoIni2 - $anyoIni1 >= 0) || !($anyoFin2 - $anyoFin1 >= 0)) {
			$message = 1;
		} elseif ( (($anyoIni2 - $anyoIni1 == 0) && !($mesIni2 - $mesIni1 >= 0)) || (($anyoFin2 - $anyoFin1 == 0) && !($mesFin2 - $mesFin1 >= 0)) ) {
			$message = 1;
		} elseif ( (($anyoIni2 - $anyoIni1 == 0) && ($mesIni2 - $mesIni1 == 0) && !($diaIni2 - $diaIni1 >=0)) || 
					(($anyoFin2 - $anyoFin1 == 0) && ($mesFin2 - $mesFin1 == 0) && !($diaFin2 - $diaFin1 >=0)) ) {
			$message = 1;
		}
	   	if ($message == 1) {
	   		$no_actualizar=1;
			echo "<h1 style='position:absolute;top:110px;left:50px;font-weight:bold'><font size='5'><u>The second date of each range must be bigger than the first date</u></font></h1>"; 
			echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/graphs.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
		}
   	}
   	?>
   	
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
   	
    google.load('visualization', '1', {packages: ['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawVisualization);

    function drawVisualization() {
	    <?php
			$q= mysql_query("select p.id_project, p.name_project, p.id_version, p.advantage, p.initial_date, p.final_date
		                                    from project p where p.id_version=(select max(P2.id_version)
		                                                      from Project P2
		                                                      where P.id_project=P2.id_project and P.deleted=0)
															  and (p.initial_date >= '$initial_date1' and p.initial_date <= '$initial_date2') 
															  and (p.final_date >= '$final_date1' and p.final_date <= '$final_date2')
											order by p.initial_date asc "); 
			$num_resultados = mysql_num_rows($q);
			$j = 0;
            $vueltas=0;		
			if ($num_resultados > 10) {
		   		$num_grupos_diez = ceil($num_resultados / 10);
		   		$vueltas=10;	
		   	}
		   	else {$vueltas=$num_resultados;}
	   		
		     $myRow = null;
		     $i = 0;
	   		 
   		 	 while ($i < $vueltas) {
   		 	 	$res = mysql_result($q, $i);
			 	$q1 = mysql_query("select max(id_version) from project where id_project = '$res'");
			 	$res2 = mysql_fetch_assoc($q1);
			 	$version = $res2['max(id_version)'];
			 	$query = mysql_query("SELECT id_project, name_project, deleted, id_version, advantage, initial_date, final_date
			 							 FROM project WHERE id_project = '$res' AND id_version = '$version'");
            	$row = mysql_fetch_assoc($query);
            	$name_proj = $row['name_project'];
            	
            	$initial_proj = $row['initial_date'];
            	$anyoInicial = substr($initial_proj, 0, 4);
				$aux5 = substr($initial_proj, 5, 6);
				$mesInicial = substr($aux5, 0, 2);
				$diaInicial = substr($initial_proj, 8, 9);
            	$initial_num = $anyoInicial . $mesInicial . $diaInicial;
            	
            	$final_proj = $row['final_date'];
            	$anyoFinal = substr($final_proj, 0, 4);
				$aux6 = substr($final_proj, 5, 6);
				$mesFinal = substr($aux6, 0, 2);
				$diaFinal = substr($final_proj, 8, 9);
				$final_num = $anyoFinal . $mesFinal . $diaFinal;
            	
            	$adv_proj = $row['advantage'];
            	$adv_num = ((($final_num - $initial_num)/ 100) * $adv_proj) + $initial_num;
            	
			 	if ($i == 0) {
			 		$myRow = $myRow . "[" . "\n";
			 		$myRow = $myRow . "['NOMBRE_PROJ', 'FECHA_INI', 'FECHA_FIN_AVANCE', 'FECHA_FIN_AVANCE', 'FECHA_FIN']," . "\n";
			 	}
			 	$myRow = $myRow . "['$name_proj', $initial_num, $initial_num, $adv_num, $final_num]";		
			 	if ($i != ($vueltas-1) ) {
			 		$myRow = $myRow . "," . "\n";
			 	} else {
			 		$myRow = $myRow . "\n". "]";
			 	}
			 	$i = $i + 1;
			 }	
			 
			 if ($num_resultados == 0) {
			 	$message = 2;
			 } else {
			 	?>
    			var dataTable = google.visualization.arrayToDataTable(<?php echo $myRow; ?>, false);	    
	        	// Draw the chart.
	       		var chart = new google.visualization.CandlestickChart(document.getElementById('visualization'));
	        	chart.draw(dataTable, {legend:'none', width:600, height:400});
	        	<?php 	 	
 	 		}	
		?>
    }
</script>
</head>
<body>

<div class="navbar navbar-inverse navbar-fixed-top">
	<div class="navbar-inner">
		<div class="container-fluid">
			<button type="button" class="btn btn-navbar" data-toggle="collapse"
				data-target=".nav-collapse">
				<span class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
			<a class="brand" href="http://localhost/bases--grupo10/homeAdmin.php?">My project manager</a>
			<div class="nav-collapse collapse">
				<p class="navbar-text pull-right">
				
					<a href="http://localhost/bases--grupo10/logout.php?" class="navbar-link">Loggout</a>
				</p>
				<ul class="nav">
					<li class="active"><a href="http://localhost/bases--grupo10/homeAdmin.php?">Home</a></li>
					<li><a href="http://localhost/bases--grupo10/about.php?">About</a></li>
					<li><a href="http://localhost/bases--grupo10/contact.php?">Contact</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
	
	
   <?php 	
	//no ha habido fallo, se muestra el grafico
	$res = array();
	$num_resultados = 0;
	$num_grupos_diez = 0;
	$tope = 0;
	$vez = 0;
	$next = 0;
	$page=1;
	
   	if ($no_actualizar==0): { ?>
		<div class="span12">
		    <h1 class="muted">Graphs > </h1>
		    <h1 style="font-weight:bold"><font size="5"><u> Planning and progress of the projects</u></font></h1>
		</div>	
	
		<?php 
		$q= mysql_query("select p.id_project, p.name_project, p.id_version, p.advantage, p.initial_date, p.final_date
	                                    from project p where p.id_version=(select max(P2.id_version)
	                                                      from Project P2
	                                                      where P.id_project=P2.id_project and P.deleted=0)
														  and (p.initial_date >= '$initial_date1' and p.initial_date <= '$initial_date2') 
														  and (p.final_date >= '$final_date1' and p.final_date <= '$final_date2') "); 
		$num_resultados = mysql_num_rows($q);
		$j = 0;
		
		if ($num_resultados > 10) {
	   		$num_grupos_diez = ceil($num_resultados / 10);	
	   	}
   		?>
   		
   		<div class="span12 offset3">
		<div id="visualization" style="font-family: Arial;border: 0 none;"></div> 
		</div>
		
   		 <?php 
		if ($vez == 0 && $num_grupos_diez > 1) { ?>
			<a class='btn btn-primary' type='button' href="http://localhost/bases--grupo10/graphs2_next.php?page=<?php echo $page;?>" style='position:absolute;top:470px;left:875px'>Next</a>
			<?php 
		} 

		if ($message == 2) {
			echo "<h1 style='position:absolute;top:160px;left:50px;font-weight:bold'><font size='5'><u>Don't exist any last version of the projects in the dates you have introduced</u></font></h1>";
		} ?>
		
		<div class="finish">  	
		    <a class="btn btn-primary pull-right" type="button" href="http://localhost/bases--grupo10/homeAdmin.php?">Finish</a>  	
		    <a class="btn btn-primary pull-left" type="button" href="http://localhost/bases--grupo10/graphs.php?" >Back</a>
	    </div>
		<?php 
		
   	}endif;	?>			
	
</body>
</html>

<?php }endif;?>
